//
//  XZStudentVC.m
//  优学荟管理系统
//
//  Created by edz on 2018/1/10.
//  Copyright © 2018年 星道三好. All rights reserved.
//

#import "XZStudentVC.h"
#import "XZStudentListVC.h"
#import "XZPaymentDetailVC.h"
#import "LLNavigationMidSearchItem.h"

@interface XZStudentVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIButton *studentList;
@property (weak, nonatomic) IBOutlet UIButton *studentstatistical;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (nonatomic,strong) UIView *allViews;
@property (nonatomic,strong) UITextField *searchTextField;
@property (nonatomic,strong) UIButton *searchButton;
@property (nonatomic, strong) UILabel *titleNameLabel;

@end

@implementation XZStudentVC
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(255,255,255)}]; //设置标题颜色
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(47, 204, 138)] forBarMetrics:(UIBarMetricsDefault)];

    
    [SVProgressHUD dismiss];
}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(255, 255, 255)] forBarMetrics:(UIBarMetricsDefault)];//设置导航栏颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(51,51,51)}];//设置标题颜色
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.scrollEnabled = NO;
    [self addChildCustomViewController];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTargat:self action:@selector(Pop) image:@"icon_back_student_sign" highImage:@""];
    [self isStudent:_studentList];
    [self setSearchName];
}

- (void)addChildCustomViewController{
    [_studentList SG_imagePositionStyle:(SGImagePositionStyleTop) spacing:8];
    [_studentstatistical SG_imagePositionStyle:(SGImagePositionStyleTop) spacing:8];
    
    //第一个
    XZStudentListVC * fristVc = [[XZStudentListVC alloc]init];
    CGRect frame = fristVc.view.frame;
    frame.size = CGSizeMake(self.scrollView.bounds.size.width, self.scrollView.bounds.size.height);
    fristVc.view.frame = frame;
    [self addChildViewController:fristVc];
    
    //第二个
    XZPaymentDetailVC * secondVc = [[XZPaymentDetailVC alloc]init];
    CGRect rect = fristVc.view.frame;
    rect.origin.x = kScreenWidth;
    secondVc.view.frame = rect;
    [self addChildViewController:secondVc];
    
    NSInteger count = self.childViewControllers.count;
    self.scrollView.contentSize = CGSizeMake(count * SCREEN_WIDTH, 0);
    [self.scrollView addSubview:fristVc.view];
    [self.scrollView addSubview:secondVc.view];
}

#pragma mark - 学生列表
- (IBAction)isStudent:(UIButton *)sender {
    self.studentList.selected = YES;
    self.studentstatistical.selected = NO;
    self.scrollView.contentOffset = CGPointMake(0, 0);
    self.titleNameLabel.hidden = YES;
    self.searchButton.hidden = NO;
    self.searchTextField.hidden = NO;
    self.allViews.backgroundColor = ColorRGB(247,245,245);
}

#pragma mark - 统计列表
- (IBAction)isStatistical:(UIButton *)sender {
    self.studentList.selected = NO;
    self.studentstatistical.selected = YES;
    self.scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    self.allViews.backgroundColor = [UIColor whiteColor];
    self.searchTextField.hidden = YES;
    self.searchButton.hidden = YES;
    self.titleNameLabel.hidden = NO;
}




- (void)setSearchName{
    
    self.allViews = [[UIView alloc]initWithFrame:CGRectMake(40, 0, kScreenWidth - 80, 30)];
    self.allViews.backgroundColor = ColorRGB(247,245,245);
    self.allViews.layer.cornerRadius = 15;
    self.allViews.clipsToBounds = YES;
    self.navigationItem.titleView = self.allViews;
    
    
    self.searchTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, self.allViews.frame.size.width, self.allViews.frame.size.height)];
    self.searchTextField.font = [UIFont systemFontOfSize:13];
    self.searchTextField.textColor = ColorRGB(102, 102, 102);
    self.searchTextField.textAlignment = NSTextAlignmentCenter;
    self.searchTextField.returnKeyType =  UIReturnKeySearch;
    self.searchTextField.clearButtonMode = UITextFieldViewModeAlways;
    self.searchTextField.delegate = self;
    [self.allViews addSubview:self.searchTextField];
    
    
    self.searchButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.searchButton setImage:[UIImage imageNamed:@"icon_serch_default"] forState:(UIControlStateNormal)];
    [self.searchButton setTitle:@"  搜索姓名、联系方式" forState:(UIControlStateNormal)];
    [self.searchButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    self.searchButton.titleLabel.font = [UIFont systemFontOfSize:13];
    self.searchButton.frame = self.searchTextField.frame;
    [self.allViews addSubview:self.searchButton];
    
    
    self.titleNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.allViews.frame.size.width, self.allViews.frame.size.height)];
    self.titleNameLabel.text = @"缴费详情";
    self.titleNameLabel.textAlignment = NSTextAlignmentCenter;
    self.titleNameLabel.font = [UIFont systemFontOfSize:17];
    self.titleNameLabel.hidden = YES;
    [self.allViews addSubview:self.titleNameLabel];
    
    
    [self.searchButton addTarget:self action:@selector(searchButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.searchTextField addTarget:self action:@selector(searchTextFieldAction:) forControlEvents:(UIControlEventEditingChanged)];
}


- (void)searchButtonAction:(UIButton *)sender{
    
    
    [self.searchTextField becomeFirstResponder];
    
}

- (void)searchTextFieldAction:(UITextField *)textField{
    
    self.searchButton.hidden = textField.text.length;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.searchTextField resignFirstResponder];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"zyStudent" object:self.searchTextField.text?self.searchTextField.text:@""];
    
    
    return YES;
}



- (void)Pop {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
